<?php
include_once('admin-load.php');
    $query = "SELECT * FROM users";
    $users = $dbna->get_results($query);
    //print_r($users);
   if (isset($_GET['action']) && $_GET['action'] == 'delete') {
       $user_id = $_GET['user_id'];
       if (isset($_GET['confirm']) && $_GET['confirm']==1) {
           delete_user($user_id);
       }
       $msg = 'Do you want to delete user? <a href="user.php?action=delete&user_id='.$user_id.'&confirm=1">YES</a> <a href="user.php">NO</a>';
   }
?>
<?php
	include_once 'header.php';
?>
            <div id="main_content">
                 <div id="content">
                    
				</div>
        <div class="container">
            <h5>User list</h5>

        </div>
				<div id="main">
                    <h2>User list</h2>
                    <?php
                        if (isset($msg)) {
                            echo '<div class="warning">'. $msg . '</div>';
                        }
                    ?>
                    <table>
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>Username</th>
                                <th>Email</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                           <?php
                                if (!empty ($users)) {
                                    foreach($users as $u) {
                                         $edit = '<a href="user-edit.php?user_id='.$u->id.'"> Edit</a>';
                                         $delete = '<a href="user.php?action=delete&user_id='.$u->id.'">Delete</a>';
                                        printf('<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>',
                                                $u->lastname . ' '. $u->firstname, $u->username, $u->email, $edit. ' '.$delete
                                               );
                                    }
                                }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
<?php 
	include_once 'footer.php';
?>